package com.wx.tgp.designpattern.FactoryModePattern;

import com.wx.tgp.designpattern.FactoryModePattern.FactoryMethod.*;
import com.wx.tgp.designpattern.FactoryModePattern.leifengFactory.*;

import java.util.Scanner;

/**
 * 工厂模式客户端类
 */
public class Client {

    public static void main(String args[]){
        Client client = new Client();
        // 工厂方法模式
//        client.factoryMethod();
        client.leiFengFactory();
    }

    public void leiFengFactory(){
        // 大学生
        LeiFengFactory factory = new StudentsFactory();
        Students students = (Students) factory.createLeiFeng();
        students.cook();
        // 志愿者
        factory = new VolunteerFactory();
        Volunteer volunteer = (Volunteer) factory.createLeiFeng();
        volunteer.Wash();
    }

    /**
     * 工厂方法模式
     */
    public void factoryMethod(){
        Factory operaFactory = null;
        Operation operation = null;
        int num1 = 0;
        int num2 = 0;
        String str3 = "";
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("请输入第一个数：");
            String str1 = scanner.nextLine();
            try{
                num1 = Integer.parseInt(str1);
            } catch (Exception e){
                System.out.println("不能输入字符，请重新输入");
                continue;
            }
            break;
        }
        while(true){
            System.out.println("请输入第二个数");
            String str2 = scanner.nextLine();
            try{
                num2 = Integer.parseInt(str2);
            } catch (Exception e){
                System.out.println("不能输入字符，请重新输入");
                continue;
            }
            break;
        }
        while(true){
            System.out.println("请输入运算字符('+','-','*','/')：");
            str3 = scanner.nextLine();
            if("+".equals(str3)||"-".equals(str3)||"*".equals(str3)||"/".equals(str3)){
                if("+".equals(str3)){
                    operaFactory = new FactorySum();
                } else if("-".equals(str3)){
                    operaFactory = new FactorySub();
                } else if("*".equals(str3)){
                    operaFactory = new FactoryMul();
                } else if("/".equals(str3)){
                    if(num2 == 0 ){
                        System.out.println("除数不能为0，谢谢！");
                    }else{
                        operaFactory = new FactoryDiv();
                    }
                }
                if(operaFactory!=null){
                    operation = operaFactory.createOperation(num1,num2);
                    System.out.println(operation.getResult());
                }
                break;
            }else{
                continue;
            }
        }
    }

}
